﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using TreaShares.Backoffice.DataLayer;

namespace TreaShares.Backoffice.Controlers
{
    [DataObjectAttribute(true)]
    public class StocksManager
    {
        [DataObjectMethodAttribute(DataObjectMethodType.Select)]
        public List<StockType> Select(string ClientId)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                Guid cId = new Guid(ClientId);
                var x = db.Stocks.Where(s => s.ClientId == cId).Select(s => s).Join(db.StockTypes, s => s.StockType, st => st.Id, (s, st) =>
                    new StockType()
                    {
                        StockId = s.Id,
                        StockCode = st.Name,
                        StockCodeId = st.Id,
                        StockName = s.Code,
                        Currency = s.Currency,
                        IssFVMultiplier = s.IssFVMultiplier,
                        IndustryBurnRate = s.IndustryBurnRate,
                        PaidUpCapital = s.StocksPaidUpCapitals.Sum(sp => sp.PaidUpCapital),
                        PaidUpCapitalDisplay = string.Empty,
                        ParValue = s.ParValue
                    });
                var dbResult = x.ToList();
                dbResult.ForEach(s => s.PaidUpCapitalDisplay = (s.PaidUpCapital == null) ? "N/A" : s.PaidUpCapital.Value.ToString("N"));
                return dbResult;
            }
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select)]
        public List<StocksPaidUpCapital> SelectStocksPaidUpCapital(string clientId, int stockId)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                Guid cId = new Guid(clientId);
                var result = db.StocksPaidUpCapitals.Where(sp => sp.StockSysId == stockId && sp.Stock.ClientId == cId).OrderBy(sp => sp.EffectiveDate).ToList();
                return result;
            }
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select)]
        public List<StockType> SelectByStockId(int stockId)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {

                var x = db.Stocks.Where(s => s.Id == stockId).Select(s => s).Join(db.StockTypes, s => s.StockType, st => st.Id, (s, st) =>
                    new StockType() { StockId = s.Id, StockCode = st.Name, StockName = s.Code, Currency = s.Currency }).ToList();
                return x;
            }
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Update)]
        public void Update(int StockId, string StockName, int StockCode, string currency, int stockCodeId, double? issFVMultiplier, double? industryBurnRate, double? parValue)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                var stock = db.Stocks.Where(s => s.Id == StockId).Single();
                stock.Code = StockName;
                stock.StockType = stockCodeId;
                stock.Currency = currency;

                stock.IssFVMultiplier = issFVMultiplier;
                stock.IndustryBurnRate = industryBurnRate;
                stock.ParValue = parValue;
                db.SubmitChanges();
            }
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select)]
        public List<StockDropdown> SelectDropdownt(string ClientId)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                Guid cId = new Guid(ClientId);
                var x = db.Stocks.Where(s => s.ClientId == cId).Select(s =>
                    new StockDropdown() { StockName = s.Code, StockValue = s.Id + "_" + s.Currency ?? string.Empty }).ToList();
                return x;
            }
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select)]
        public List<TreaShares.Backoffice.DataLayer.StockType> SelectStockTypes(string ClientId)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                Guid cId = new Guid(ClientId);
                return db.StockTypes.Where(st => st.ClientId == cId).Select(st => st).ToList();
            }
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Insert)]
        public void Insert(string stockName, int stockCode, Guid cId, string currency, double? issFVMultiplier, double? industryBurnRate, float parValue)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                db.Stocks.InsertOnSubmit(new Stock() { Code = stockName, StockType = stockCode, ClientId = cId, Currency = currency, IssFVMultiplier = issFVMultiplier, IndustryBurnRate = industryBurnRate, ParValue = parValue });
                db.SubmitChanges();
            }
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Delete)]
        public void Delete(int StockId)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                db.Stocks.DeleteOnSubmit(db.Stocks.Where(s => s.Id == StockId).Single());
                db.SubmitChanges();
            }
        }


        internal void InsertPaidUpCapital(int stockId, int paidUpCapital, DateTime effectiveDate)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                db.StocksPaidUpCapitals.InsertOnSubmit(new StocksPaidUpCapital() { EffectiveDate = effectiveDate, StockSysId = stockId, PaidUpCapital = paidUpCapital });
                db.SubmitChanges();
            }
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Delete)]
        public void DeletePaidUpCapital(int id)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                db.StocksPaidUpCapitals.DeleteOnSubmit(db.StocksPaidUpCapitals.Where(s => s.Id == id).Single());
                db.SubmitChanges();
            }
        }
    }


    public class StockType
    {
        public int StockId { get; set; }
        public string StockName { get; set; }
        public string StockCode { get; set; }
        public int StockCodeId { get; set; }
        public string Currency { get; set; }
        public double? PaidUpCapital { get; set; }
        public string PaidUpCapitalDisplay { get; set; }
        public double? IssFVMultiplier { get; set; }
        public double? IndustryBurnRate { get; set; }
        public DateTime EffectiveDate { get; set; }
        public double? ParValue { get; set; }
    }
    public class StockDropdown
    {
        public string StockName { get; set; }
        public string StockValue { get; set; }
    }
}